package com.micro.frog.fortune.executor.cwl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.micro.frog.fortune.dao.entity.CwlRecordPo;
import com.micro.frog.fortune.dao.mapper.CwlRecordMapper;
import com.micro.frog.fortune.model.base.Resp;
import com.micro.frog.fortune.model.cwl.CwlRecordReq;
import com.micro.frog.fortune.sdk.cwl.CwlApi;
import com.micro.frog.fortune.sdk.cwl.model.CwlApiReq;
import com.micro.frog.fortune.sdk.cwl.model.CwlApiResp;
import com.micro.frog.fortune.sdk.cwl.model.CwlApiResp.ResultDTO;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 同步历史数据
 *
 * @author jason.lv
 */
@Slf4j
@Service
public class CwlSyncExecutor {

    @Autowired
    private CwlApi cwlApi;
    @Autowired
    private CwlRecordMapper cwlRecordMapper;

    private String start(Integer year){
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.YEAR, year);
        Calendar calendar = DateUtil.beginOfYear(instance);
        return DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
    }

    private String end(Integer year){
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.YEAR, year);
        Calendar calendar = DateUtil.endOfYear(instance);
        return DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
    }

    public Resp execute(CwlRecordReq request) {

        int year = 2003;
        int now = DateUtil.year(new Date());

        while (year <= now){
            CwlApiReq req = new CwlApiReq();
            req.setName(request.getName().getCode());
            req.setIssueStart(request.getIssueStart());
            req.setIssueEnd(request.getIssueEnd());
            req.setDayStart(start(year));
            req.setDayEnd(end(year));
            CwlApiResp cwlApiResp = cwlApi.list(req);
            if (CollUtil.isNotEmpty(cwlApiResp.getResult())){
                for (ResultDTO record : cwlApiResp.getResult()) {
                    CwlRecordPo po = new CwlRecordPo();
                    po.setCode(record.getCode());
                    po.setName(record.getName());
                    po.setDate(record.getDate());
                    po.setRed(record.getRed());
                    po.setBlue(record.getBlue());
                    po.setType(request.getName());
                    po.setPool(Convert.toBigDecimal(record.getPoolMoney()));
                    po.setSales(Convert.toBigDecimal(record.getSales()));
                    this.saveOrUpdate(po);
                }
            }
            year++;
        }
        return Resp.generate();
    }

    private CwlRecordPo get(String code) {
        QueryWrapper<CwlRecordPo> wrapper = new QueryWrapper<>();
        wrapper.eq("code", code);
        return cwlRecordMapper.selectOne(wrapper);
    }

    private void saveOrUpdate(CwlRecordPo po) {
        CwlRecordPo existPo = get(po.getCode());
        if (Objects.nonNull(existPo)) {
            po.setId(existPo.getId());
            cwlRecordMapper.updateById(po);
            return;
        }
        cwlRecordMapper.insert(po);


    }


}

